package com.zhyl.demo.controller;

import com.zhyl.demo.entity.Health;
import com.zhyl.demo.entity.SendMessage;
import com.zhyl.demo.service.HealthService;
import com.zhyl.demo.service.SendMessageService;
import com.zhyl.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * @author yang3
 */
@RestController
@RequestMapping("/health")
public class HealthController {
    private final HealthService healthService;
    private final SendMessageService sendMessageService;

    @Autowired
    private HealthController(HealthService healthService,
                             SendMessageService sendMessageService){
        this.healthService = healthService;
        this.sendMessageService = sendMessageService;
    }

    @RequestMapping("/addPatientHealth")
    public Result addPatientHealth(@RequestBody Health health)
    {
        if (health==null){
            return Result.error("-1","参数错误");
        }
        Boolean insert = healthService.insert(health);
        if (insert){
            SendMessage sendMessage = new SendMessage();
            sendMessage.setMessage("您的体检报告已生成，请及时查看");
            sendMessage.setUserId(health.getPatientId());
            sendMessageService.sendMessage(sendMessage);
            return Result.success();
        }else {
            return Result.error("-1","添加失败");
        }
    }

    @RequestMapping("/selectByPatientId")
    public Result selectByPatientId(int patientId){
        if (patientId<=0){
            return Result.error("-1","参数错误");
        }
        ArrayList<Health> health = healthService.selectByPatientId(patientId);
        return Result.success(health);
    }

    @RequestMapping("/getHealthDetail")
    public Result getHealthDetail(Integer healthId){

        if (healthId==null){
            return Result.error("-1","参数错误");
        }
        Health health = healthService.getHealthDetail(healthId);
        System.out.println(health);
        if (health==null){
            return Result.error("-1","数据获取失败");
        }
        return Result.success(health);
    }

    @PostMapping("/test")
    public Result test(@RequestBody Health health) {
        System.out.println(health);
        return Result.success(health);
    }

}
